/*
 * @Author: llch
 * @Date: 2020-09-15 10:19:55
 * @LastEditTime: 2020-09-16 11:26:21
 * @LastEditors: Please set LastEditors
 * @Description: 声音管理器
 * @FilePath: \tower_defense_demo\assets\scripts\sound_mgr.ts
 */

class SoundManager {
    // 全局单例
    public static readonly Instance: SoundManager = new SoundManager();

    public b_music_mute: number = 0;    // 表示我们的背景音乐是否静音，0为没有静音，1为静音;
    public b_effect_mute: number = 0;   // 表示我们的音效是否静音，0为没有静音，1为静音;
    public bg_music_name: string = undefined;   // 保存我们的背景音乐的文件名称的; 
    public bg_music_loop: boolean = false;      // 是否循环播放

    // 构造函数 
    constructor() {
        this.initSound();
    }

    // 设置背景音乐播放状态
    public set_music_mute(b_mute: number): void {
        if (this.b_music_mute == b_mute) {  // 播放状态未改变
            return;
        }
        this.b_music_mute = (b_mute) ? 1 : 0;

        // 如果是静音，将音量调为0，否则为1
        if (this.b_music_mute === 1) {  // 静音
            cc.audioEngine.stopMusic(); // 停止背景音乐播放
        } else if (this.b_music_mute === 0) {// 打开声音
            if (this.bg_music_name) {
                this.play_music(this.bg_music_name, this.bg_music_loop);
            }
            cc.audioEngine.setMusicVolume(1);
        }    

        // 将背景音乐播放状态存到本地
        cc.sys.localStorage.setItem("music_mute", this.b_music_mute);
    }

    // 设置音效播放状态
    public set_effect_mute(b_mute: number): void {
        if (this.b_effect_mute == b_mute) {
            return;
        }
        this.b_effect_mute = (b_mute) ? 1 : 0;
        
        // 将特效播放状态存到本地
        cc.sys.localStorage.setItem("effect_mute", this.b_effect_mute);
    }

    // 停止背景音乐
    public stop_music(): void {
        cc.audioEngine.stopMusic();
        this.bg_music_name = null;
    }

    // 播放背景音乐
    public play_music(file_name: string, loop: boolean): void {
        cc.audioEngine.stopMusic();         // 停止当前播放的背景音乐

        this.bg_music_name = file_name;           // 保存当前播放的背景音乐
        this.bg_music_loop = loop;

        if (!this.b_music_mute) {
            cc.resources.load(file_name, cc.AudioClip, null, function (err, clip: cc.AudioClip) {
                if (err) {
                    cc.error("====> 加载背景音乐资源失败!");
                    cc.log(err);
                    return;
                }
                cc.audioEngine.playMusic(clip, loop);
            });
        }
    }

    // 播放音效
    public play_effect(file_name: string): void {
        if (this.b_effect_mute) {   // 音效静音，直接返回
            return;
        }

        cc.resources.load(file_name, cc.AudioClip, null, function (err, clip: cc.AudioClip) {
            if (err) {
                cc.error("====> 加载音效资源失败!");
                cc.log(err);
                return;
            }
            cc.audioEngine.playEffect(clip, false);
        });
    }

    // 从本地保存中初始化声音设置
    public initSound(): void {
        // 从本地获取用户的背景声音设置
        let music_mute: number = cc.sys.localStorage.getItem("music_mute");
        if (!music_mute) {
            music_mute = 0;
        }
        this.set_music_mute(music_mute);

        // 从本地获取用户的音效设置
        let effect_mute: number = cc.sys.localStorage.getItem("effect_mute");
        if (!effect_mute) {
            effect_mute = 0;
        }
        this.set_effect_mute(effect_mute);
    }
} 

export const SoundMgr = SoundManager.Instance;